﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recRestructurform : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                recRestructurRepo repoRes = new recRestructurRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recInvoiceRepo repoInVoice = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recDebtor> debtors = repoRes.GetListDebtOnInvoice();

                ddlDebtor.Items.Clear();
                debtors.ForEach(delegate(recDebtor debtor)
                {
                    ddlDebtor.Items.Add(new ListItem(debtor.DebtorCode + " - " + debtor.DebtorName, debtor.DebtorID.ToString()));
                });
                ddlDebtor.Items.Insert(0, new ListItem("-- pilih debitur --", string.Empty));

                recActionRestructurRepo repoActionRes = new recActionRestructurRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recActionRestructur> listActionRes = repoActionRes.GetList();

                ddlJenisRes.Items.Clear();
                listActionRes.ForEach(delegate(recActionRestructur actionRes)
                {
                    ddlJenisRes.Items.Add(new ListItem(actionRes.ActionRestructurName, actionRes.ActionRestructurID.ToString()));
                });

                ddlJenisRes.Items.Insert(0, new ListItem("-- pilih jenis restruktur --", string.Empty));

                if (id > 0) // Edit an existing item
                {
                  //  recRestructurRepo repoRes = new recRestructurRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recRestructur obj = repoRes.DetailFullObjectRestructure(id);

                    if (obj != null)
                    {
                        List<recInvoice> invoices = repoInVoice.GetListEdit(obj.DebtorID.ToString(),id.ToString());
                        ddlInvoice.Items.Clear();
                        invoices.ForEach(delegate(recInvoice invoice)
                        {
                            ddlInvoice.Items.Add(new ListItem(invoice.InvoiceNo , invoice.InvoiceNo.ToString()));
                        });
                        ddlInvoice.Items.Insert(0, new ListItem("-- pilih no. invoice --", string.Empty));

                        //ddlJenisRes.SelectedValue = obj.ActionRestructurID;
                        ddlDebtor.Items.FindByValue(obj.DebtorID.ToString()).Selected = true;
                        ddlJenisRes.Items.FindByValue(obj.ActionRestructurID.ToString()).Selected = true;
                        txtNoSK.Text = obj.LetterNo;
                        txtTglSK.Text = obj.LetterDate.HasValue ? obj.LetterDate.Value.ToString("dd/MM/yyyy") : string.Empty;
                        txtJmlRestruktur.Text = (decimal.Parse(obj.RestructurValues.ToString().TrimEnd('0'))).ToString();
                        txtJmlTagihan.Text = (decimal.Parse(obj.SumValues.ToString().TrimEnd('0'))).ToString();
                        ddlInvoice.SelectedValue = obj.InvoiceNo;
                        hfID.Value = id.ToString();
                    }
                }

                GetPrivilege();
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        //ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}